﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using BooksManager.Data;
using BooksManager.Entities;

using Microsoft.AspNetCore.Mvc;

namespace BooksManager.Controllers
{
    [Route("api/test")]
    [ApiController]
    public class TestController : ControllerBase
    {
        private readonly AppDbContext _db;

        public TestController(AppDbContext db)
        {
            _db = db;
        }

        [HttpGet("exception")]
        public void ThrowException()
        {
            throw new Exception("测试异常");
        }

        [HttpGet("reset")]
        public async Task ResetAsync()
        {
            await _db.Database.EnsureDeletedAsync();
            await _db.Database.EnsureCreatedAsync();

            _db.Add(new Book
            {
                Name = "C程序设计语言",
                Author = "Brian W.Kernighan",
                PublicationDate = new DateTime(2004, 1, 1),
                Tags = new[] { "C语言", "编程语言", "基础" },
                DoubanScore = 94,
                Page = 258,
            });

            _db.Add(new Book
            {
                Name = "计算机程序设计艺术（第1卷）",
                Author = "Donald E. Knuth",
                PublicationDate = new DateTime(2002, 1, 1),
                Tags = new[] { "设计模式", "编程思想", "进阶" },
                DoubanScore = 95,
                Page = 650,
            });

            _db.Add(new Book
            {
                Name = "Linux内核设计与实现",
                Author = "Robert Love",
                PublicationDate = new DateTime(2011, 6, 12),
                Tags = new[] { "Linux编程", "操作系统", "进阶" },
                DoubanScore = 86,
                Page = 352,
            });

            _db.Add(new Book
            {
                Name = "C++程序设计语言",
                Author = "Bjarne Stroustrup",
                PublicationDate = new DateTime(2002, 7, 1),
                Tags = new[] { "C++", "编程语言", "基础" },
                DoubanScore = 88,
                Page = 905,
            });

            _db.Add(new Book
            {
                Name = "C# 8.0 in a Nutshell",
                Author = "Joseph Albahari / Eric Johannsen",
                PublicationDate = new DateTime(2020, 5, 19),
                Tags = new[] { "C#", "编程语言", "基础" },
                DoubanScore = 82,
                Page = 1088,
            });

            _db.Add(new Book
            {
                Name = "SQL基础教程",
                Author = "MICK",
                PublicationDate = new DateTime(2017, 6, 1),
                Tags = new[] { "数据库", "基础" },
                DoubanScore = 90,
                Page = 320,
            });

            await _db.SaveChangesAsync();
        }
    }
}
